package itbour.onetouchshow.widget;

import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;

import com.google.gson.Gson;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import itbour.onetouchshow.R;
import itbour.onetouchshow.adapter.InchingChangeTextColorAdapter;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.utils.SpUtils;

/**
 * Created by guoh on 2018/8/3.
 * 功能描述：字体属性设置布局
 * 需要的参数：
 */
public class FontAttributeSettingLayout extends LinearLayout {

    @BindView(R.id.tv_edit_letter)
    TextView tvEditLetter;
    @BindView(R.id.tv_font_font)
    TextView tvFontFont;
    @BindView(R.id.tv_font_size)
    TextView tvFontSize;
    @BindView(R.id.tv_font_color)
    TextView tvFontColor;
    @BindView(R.id.tv_delete_letter)
    TextView tvDeleteLetter;
    @BindView(R.id.i_btn_save)
    ImageButton iBtnSave;

    private Font font;
    private Integer fontSize;
    private Integer r;
    private Integer g;
    private Integer b;
    private onAttributeItemClickListener ls;

    public FontAttributeSettingLayout(Context context) {
        super(context);
        initView(context);
    }

    public FontAttributeSettingLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }


    public FontAttributeSettingLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_font_attribute_setting, this);
        ButterKnife.bind(view, this);
    }

    @OnClick({R.id.tv_edit_letter, R.id.tv_font_font, R.id.tv_font_size, R.id.tv_font_color, R.id.tv_delete_letter, R.id.i_btn_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_edit_letter:
                if (ls != null) ls.onEditLetterClick();
                break;
            case R.id.tv_font_font:
                showFontPopWindow(getContext());
                break;
            case R.id.tv_font_size:
                showFontSizePopWindow(getContext());
                break;
            case R.id.tv_font_color:
                showColorPopWindow(getContext());
                break;
            case R.id.tv_delete_letter:
                if (ls != null) ls.onDeleteLetterClick();
                break;
            case R.id.i_btn_save:
                if (ls != null) ls.onSaveLetterClick();
                break;
        }
    }

    public void setOnAttributeItemClickListener(onAttributeItemClickListener ls) {
        this.ls = ls;
    }

    private void showColorPopWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_attribute_font_color, null);
        ImageView close = view.findViewById(R.id.iv_close);
        RecyclerView recyclerView = view.findViewById(R.id.color_recy);
        PopupWindow popupWindow = createPopWindow(view);
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        //设置数据
        HomeDetailslBean detailsBean = new Gson().fromJson(SpUtils.getInstance().getString(AppConst.APPBASEINFO), HomeDetailslBean.class);
        List<List<Integer>> colorList = detailsBean.getColorList();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 6);
        recyclerView.setLayoutManager(gridLayoutManager);
        InchingChangeTextColorAdapter adapter = new InchingChangeTextColorAdapter(context, colorList);
        recyclerView.setAdapter(adapter);
        if (r != null && g != null && b != null) {
            for (int i = 0; i < colorList.size(); i++) {
                List<Integer> color = colorList.get(i);
                int m_r = color.get(0);
                int m_g = color.get(1);
                int m_b = color.get(2);
                if (m_r == r && m_b == b && m_g == g) {
                    adapter.setNowSelect(i);
                    break;
                }
            }
        }
        adapter.setOnTextColorItemClick(new InchingChangeTextColorAdapter.OnTextColorItemClick() {
            @Override
            public void onTextColorClick(int r, int g, int b, String type) {
                if (ls != null) {
                    ls.onFontColorClick(r, g, b, type);
                }
            }
        });
    }

    private void showFontSizePopWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_attribute_font_size, null);
        ImageView close = view.findViewById(R.id.iv_close);
        SeekBar seekBar = view.findViewById(R.id.sk_progress);
        PopupWindow popupWindow = createPopWindow(view);
        if (fontSize != null) {
            seekBar.setProgress(fontSize);
        }
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (ls != null) {
                    ls.onFontSizeChange(seekBar.getProgress());
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    private void showFontPopWindow(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_attribute_font_font, null);
        ImageView close = view.findViewById(R.id.iv_close);
        TextView tvDefaultFont = view.findViewById(R.id.tv_default_font);
        TextView tvBlackFont = view.findViewById(R.id.tv_black_font);
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "Fonts/FZHei_B01S.ttf");
        tvBlackFont.setTypeface(typeface);
        PopupWindow popupWindow = createPopWindow(view);
        if (font != null) {
            switch (font) {
                case defaultFont:
                    tvBlackFont.setBackground(context.getResources().getDrawable(R.drawable.font_unselect_translate));
                    tvDefaultFont.setBackground(context.getResources().getDrawable(R.drawable.font_select_style));
                    break;
                case blackFont:
                    tvBlackFont.setBackground(context.getResources().getDrawable(R.drawable.font_select_style));
                    tvDefaultFont.setBackground(context.getResources().getDrawable(R.drawable.font_unselect_translate));
                    break;
            }
        }
        close.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        tvDefaultFont.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvBlackFont.setBackground(context.getResources().getDrawable(R.drawable.font_unselect_translate));
                tvDefaultFont.setBackground(context.getResources().getDrawable(R.drawable.font_select_style));
                if (ls != null) {
                    ls.onFontClick(Font.defaultFont);
                }
            }
        });
        tvBlackFont.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tvBlackFont.setBackground(context.getResources().getDrawable(R.drawable.font_select_style));
                tvDefaultFont.setBackground(context.getResources().getDrawable(R.drawable.font_unselect_translate));
                if (ls != null) {
                    ls.onFontClick(Font.blackFont);
                }
            }
        });
    }

    private PopupWindow createPopWindow(View view) {
        PopupWindow popupWindow = new PopupWindow(view);
        popupWindow.setWidth(LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        popupWindow.showAsDropDown(this, Gravity.BOTTOM, 0, 0);
        return popupWindow;
    }

    //设置字体
    public void setFont(Font font) {
        if (font == null) this.font = null;
        if (font == null) return;
        switch (font) {
            case defaultFont:
                this.font = Font.defaultFont;
                break;
            case blackFont:
                this.font = Font.blackFont;
                break;
        }
    }

    //设置字体大小
    public void setFontSize(int size) {
        this.fontSize = size;
    }

    //设置字体颜色
    public void setFontColor(Integer r, Integer g, Integer b) {
        this.r = r;
        this.g = g;
        this.b = b;
    }

    //清除所有的属性
    public void clearAllAttribute() {
        this.font = null;
        this.fontSize = null;
        this.r = null;
        this.g = null;
        this.b = null;
    }

    public enum Font {
        defaultFont("default"),
        blackFont("FZHei_B01S");

        private String name;

        Font(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

    public interface onAttributeItemClickListener {
        void onEditLetterClick();

        void onFontClick(Font font);

        void onDeleteLetterClick();

        void onSaveLetterClick();

        void onFontColorClick(int r, int g, int b, String type);

        void onFontSizeChange(int fontSize);
    }
}
